#include "GameSettings.h"
#include "ZStream.h"

ZVariant Settings::GetValue(STL::string i_key) const
{
	MapCit it = m_map.find(i_key);

	if(it != m_map.end())
		return (*it).second;

	assert(0 && "Setting not found!");
	return ZVariant();
}

bool Settings::Load(const char* const i_filename)
{
	ZStream file;

	if(!file.Open(i_filename, STD::ios_base::in))
		return false;

	Deserialize(file);

	file.Close();

	return true;
}

ZStream& Settings::Serialize(ZStream& o_stream) const
{
	return o_stream;
}

ZStream& Settings::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();

	STL::string		name;
	bool			ended = false;

	while(!ended)
	{	
		i_stream.SkipLine();

		i_stream.ReadAttribute(name);

		if(name.empty())
			break;

		//check the value type
		switch(name[0])
		{
			case 'I':
			case 'i':
			{
				int value;
				i_stream.ReadAttribute(value);
				//m_map[name].Int = value;
				m_map[name].Set(value);
				break;
			}
			case 'F':
			case 'f':
			{
				float value;
				i_stream.ReadAttribute(value);
				m_map[name].Set(value);
				//m_map[name].Float = value;
				break;
			}
			case 'B':
			case 'b':
			{
				bool value;
				i_stream.ReadAttribute(value);
				//m_map[name].Bool = value;
				m_map[name].Set(value);
				break;
			}
			case 'C':
			case 'c':
			{
				char value;
				i_stream.ReadAttribute(value);
				//m_map[name].Char = value;
				m_map[name].Set(value);
				break;
			}
			default:
			{
				if(i_stream.IsEnd())
					ended = true;
				else
					assert(0 && "No value type found in the name");
			}
		}

		i_stream.SkipLine();
		
	}

	i_stream.SkipLine();

	return i_stream;
}